// getSeatHours

import axios from "axios";
import dayjs from "dayjs";

/**
 *  $output = [
 *    'start' => $start,
 *    'end' => $end,
 *    'count' => count($result),
 *    'content' => $result,
 *    'users' => $users,
 *    'usersGrouped' => $users_grouped,
 *    // 'sql' => $sql,
 *  ];
 */

export default function (
  start = dayjs().subtract(24, "hour").format("YYYY-MM-DD HH:mm:ss"),
  end = dayjs().format("YYYY-MM-DD HH:mm:ss")
) {
  return new Promise((resolve, reject) => {
    axios
      .post("api/seat_hours_get.php", { start, end })
      .then((response) => {
        let content = response.data.content.map((cell) => {
          let end = cell.endTime ? dayjs(cell.endTime) : dayjs();
          let minutes = end.diff(cell.startTime, "minute");
          let hours = end.diff(cell.startTime, "hour", true);
          return {
            number: cell.recordID,
            name: cell.userName,
            rank: cell.controlLevel,
            qualify: cell.qualification,
            license: cell.controlLevelId < 3 ? "是" : "否",
            seat: cell.seatCode,
            sector: cell.seatCode,
            // sector: cell.sectorCodes,
            post: cell.seatPost,
            role: cell.seatRole,
            begin: cell.startTime,
            end: cell.endTime
              ? cell.endTime
              : end.format("YYYY-MM-DD HH:mm:ss"),
            minutes: minutes,
            hours: hours,
            hoursRaw: hours,
            wage: 0,
            salary: 0,
            ratio: 0,
          };
        });
        console.log("TrainingSummary", "getSeatHours", content);
        resolve(content);
      })
      .catch((error) => {
        console.error("getSeatHours", `from ${start} to ${end}`, error);
        reject(error);
      });
  });
}
